package day3;

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param numbers int整型一维数组 
     * @return int整型
     */
    public static int MoreThanHalfNum_Solution (int[] numbers) {
        // write code here
        int count = numbers.length / 2;
        int num = 0;
        HashMap<Integer,Integer> map = new HashMap<>();
        for(int i = 0; i < numbers.length; i++) {
            if(!map.containsKey(numbers[i])) {
                map.put(numbers[i],1);
            }else {
                map.put(numbers[i],map.get(numbers[i])+1);
            }
        }
        for(int i = 0; i < numbers.length; i++) {
            if(map.get(numbers[i]) > count) {
                num = numbers[i];
                break;
            }
        }
        return num;
    }

    public static void main(String[] args) {
        int[] arr = new int[]{1,2,3,2,2,2,5,4,2};
        int a = MoreThanHalfNum_Solution(arr);
        System.out.println(a);
    }
}